<?php

/**
 * 图片裁剪
 * @param $src
 * @param $rate  宽是高的多少倍
 * @return bool
 */
function editImg($src, $rate, $min_y=200) {
    $image = $src; // 原图
    $imgstream = file_get_contents(str_replace('\\', '/', $image));
    $im = imagecreatefromstring($imgstream);
    $x = imagesx($im);//获取图片的宽
    $y = imagesy($im);//获取图片的高

    if($x > $y * $rate){
        //图片宽大于高
        $y = $y < $min_y ? $min_y : $y;
        $thumbw = $y * $rate;
        $thumbh = $y;
    } else if ($x < $y * $rate) {
        //图片高大于等于宽
        $x = $x < $min_y*$rate ? $min_y*$rate : $x;
        $thumbw = $x;
        $thumbh = $x / $rate;
    } else {
        return true;
    }
    if(function_exists("imagecreatetruecolor")) {
        $dim = imagecreatetruecolor($thumbw, $thumbh); // 创建目标图gd2
    } else {
        $dim = imagecreate($thumbw, $thumbh); // 创建目标图gd1
    }
    imageCopyreSampled ($dim,$im,0,0,0,0,$thumbw,$thumbh,$thumbw,$thumbh);
	header ("Content-type: image/jpeg");
    imagejpeg ($dim, $image);
    imagedestroy($dim);
}
//原点 2020年7月13日10:20:32 解析时间配置
function set_layDate($value,$format){
    $format = explode('_',$format);
    switch ($format[0]){
        case "year":
        case "month":
        case "date":
        case "datetime":
            if($format[1] == 1){
                $value = strtotime($value);
            }else{
                $value = explode(' - ',$value);
                $value[0] = strtotime($value[0]);
                $value[1] = strtotime($value[1]);
                $value = implode(',',$value);
            }
            break;
        case "time":
            if($format[1] != 1){
                $key = explode(' - ',$value);
                $start = strtotime($key[0]);
                $end = strtotime($key[1]);
                if($end < $start){
                    return false;
                }
                $value = implode(',', $key);
            }
            break;
    }
    return $value;
}

//原点 2020年7月27日15:37:16  获取时间配置
function get_layDate($value,$format){
    $format = explode('_',$format);
    switch ($format[0]){
        case "year":
        case "month":
        case "date":
        case "datetime":
            $s = ["year"=>'Y',"month"=>'Y-m',"date"=>'Y-m-d',"datetime"=>'Y-m-d H:i:s'];
            if($format[1] == 1){
                $value  = date($s[$format[0]],$value);
            }else{
                $value = explode(',',$value);
                $value[0] = date($s[$format[0]],$value[0]);
                $value[1] = date($s[$format[0]],$value[1]);
                $value = implode(' - ',$value);
            }
            break;
        case "time":
            if($format[1] != 1){
                $value = implode(' - ', explode(',',$value));
            }
            break;
            break;
    }

    return $value;
}

/**
 * @param string $group 配置分组
 * @return string
 * 获取配置的分组
 */
function get_config_group($group = 0)
{
    $list = config('config_group_list');
    return $group ? $list[$group] : $list;
}

/**
 * 获取配置的类型
 * @param string $type 配置类型
 * @return string
 * 获取配置类型
 */
function get_config_type($type = 0)
{

    $list = config('config_type_list');
    return $type ? $list[$type] : $list;

}


/**
 * @param $string
 * @return array|array[]|false|string[]
 *解析字符串为数组
 */
function parse_config_attr($string)
{
    $array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
    if (strpos($string, ':')) {
        $value = array();
        foreach ($array as $val) {
            list($k, $v) = explode(':', $val);
            $value[$k] = $v;
        }
    } else {
        $value = $array;
    }
    return $value;
}

